/*
   Title: MASTER.do
   Author: Clotaire Boyer
   Created: Nov 2023
   Description: Runs All the Cleaning and Analysis Do files of: 
   "Financial Spillover Effects from Electronic Government Transfers:  
   Evidence from an At-Scale Experiment in Indonesia"
*/

/*----------------------------------------------------*/
    /* Section 0: Setup  */
/*----------------------------------------------------*/

** Parameters
* Set seed for perfect replication
set seed 11022022

* set desired number of replications for RI p-values
global RI = 1000
assert 1 <= $RI & $RI <= 1000

** Global and Paths

/* To run the do files set the paths "base" and "data", and mount the encrypted volumes and set
    the respective paths for "agent_data", "agents_mar_18" */

*** Manual Paths
* paths to base directories
local user "`c(username)'"
global dropboxbase                         "/Users/`user'/Dropbox (MIT)/J-PAL Raskin Transition"

// CHANGE LINES 35, 39, 42,44 DIRECTLY FOR ENCRYPTED DATA LOCATIONS
* mount "J-PAL Raskin Transition Questionnaires & Data/Agent ratio experiment/Agent Data"
// Then set the path "agent_data" to the location of the mounted volume
global agent_data                   "/Volumes/MOUNT1"

* Then mount "J-PAL Raskin Transition Questionnaires & Data/Agent ratio experiment/Agent Data March 2018"
// and then set the path "agents_mar_18" to the location of the mounted volume
global agents_mar_18                "/Volumes/MOUNT2"

* Then mount "J-PAL Raskin Transition Questionnaires & Data/SUSENAS UDB MERGE
// Set udb to location of mounted UDB merge data containers below
global udb_sep18                          "/Volumes/UDB18"
global udb_mar19                          "/Volumes/NO NAME"

*** Auto paths
// CHANGE LINE 43 or 29 DIRECTLY FOR CODE/OUTPUT/PROCESSING LOCATION (Typically where you saved
// our replication package on your local computer)
global rundir                       "$dropboxbase/Public_Package_Financial_BPNT"
global cleaned                      "$rundir/01_Data/cleaned"
global dodir                        "$rundir/02_Dofiles"
global log                          "$rundir/03_Logs"
global output                       "$rundir/04_Output"
global importable                   "$rundir/01_Data/importable"

** Data location (Except for the Agent Data)
// CHANGE LINE 58/59 DIRECTLY FOR THE NON-ENCRYPTED DATA LOCATIONS
global data                         "/Users/`user'/Dropbox (MIT)/J-PAL Raskin Transition Questionnaires & Data"
global importable                   "$dropboxbase/10_Analysis&Results/Impact Eval Analysis/01_Data/01_Importable"
cd "$dropboxbase"

** Programs
** Assert necessary programs are installed
which pdslasso
which estout
which grstyle
which randinference
// If code 64 to 67 fails please run setup.do (see Read-Me for full instructions)

// Note: As we publish log files for transparency we choose not to run any tab
// and summ commands in the public package to guarantee perfect data privacy.

/*----------------------------------------------------*/
    /* Section 1: Cleaning */
/*----------------------------------------------------*/

** Optional/Run once: if user receives raw data in dbf and needs to convert as .dta files
* if you are given .dta file, simply add them directly under 01_Data/importable
* do "$dodir/Cleaning/raw_to_importable.do"

** Create 1000 iterations of randomization assignments to calculate RI p-values
do "$dodir/Cleaning/fake_kab_radomization_iterate.do"

** Match unmatched agents to area codes
do "$dodir/Cleaning/match_unmatched_agents.do"

** Create podes 2018 control variables and 2019 outcomes
do "$dodir/Cleaning/clean_podes.do"

** Clean agent data and creates village-level agents dataset
do "$dodir/Cleaning/clean_agents_village.do"

** Clean former SUSENAS datasets for lasso controls
do "$dodir/Cleaning/clean_baseline_susenas.do"

** Clean March 2018 SUSENAS for outcome at baseline
do "$dodir/Cleaning/clean_susenas_finance_mar18.do"

** Clean Mar 2019 SUSENAS (Household outcomes)
do "$dodir/Cleaning/clean_susenas_finance_mar19.do"

** Clean UDB data
do "$dodir/Cleaning/clean_udb.do"

** Create lasso-prepped datasets
do "$dodir/Cleaning/lasso_prep_finance.do"

/*----------------------------------------------------*/
  /* Section 2: Analysis  */
/*----------------------------------------------------*/

** First stage agents outcomes
do "$dodir/Analysis/agents.do"

** SUSNEAS 19 outcomes household level outcomes
do "$dodir/Analysis/household_susenas.do"

** SUSNEAS 19 outcomes individual level outcomes
do "$dodir/Analysis/individual_susenas.do"

** PODES village outcomes
do "$dodir/Analysis/villages_podes.do"

/*----------------------------------------------------*/
  /* Section 3: Output  */
/*----------------------------------------------------*/

cd "$output" 
 
** Output regressions
do "$dodir/Analysis/output_tables.do"

// DONE
